<template>
  <div class="form_wrap">
    <div class="form_list">
      <el-input v-model="searchText"></el-input>
      <el-date-picker
        v-model="value2"
        popper-class="mydate"
        type="datetimerange"
        :picker-options="pickerOptions"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        align="right"
      >
      </el-date-picker>
      <el-button type="success" @click="tosearch">查询</el-button>
    </div>
    <el-button type="primary" @click="open">添加</el-button>
    <el-dialog :title="dtitle" :visible.sync="adminFlag">
      <el-form
        :rules="ruelObj"
        :model="formObj"
        ref="adminForm"
        label-width="90px"
        label-position="left"
      >
        <el-form-item label="用户名：" prop="userName">
          <el-input v-model="formObj.userName"></el-input>
        </el-form-item>
        <el-form-item label="姓名：" prop="turename">
          <el-input v-model="formObj.turename"></el-input>
        </el-form-item>
        <el-form-item label="手机号:" prop="phone">
          <el-input v-model="formObj.phone"></el-input>
        </el-form-item>
        <el-form-item label="全选:">
          <el-select v-model="formObj.power">
            <el-option label="系统管理员" value="1"></el-option>
            <el-option label="课程管理员" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="success" @click="sureAdd">{{ dBtn }}</el-button>
        <el-button type="denger" @click="close()">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { _AddAdmin, _editAdmin } from '../../api/admin'
import { cloneDeep } from '../../utils/tool'
export default {
  data () {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      dBtn: '确认添加',
      dtitle: '添加管理员',
      adminFlag: false,
      value2: '',
      ruelObj: {
        userName: { required: true, message: '用户名必填', trigger: 'blur' },
        turename: { required: true, message: '姓名必填', trigger: 'blur' },
        phone: { required: true, message: '手机号必填', trigger: 'blur' }
      },
      formObj: {
        //添加管理员发送给后台的数据
        userName: '',
        turename: '',
        phone: '',
        password: '123456',
        power: ''
      },
      searchText: '',
      editRow: {}
    }
  },
  watch: {
    value2 (newVal, oldVal) {
      console.log(newVal)
    }
  },
  created () {
    this.$bus.$on('toedit', row => {
      let myrow = cloneDeep(row) //深克隆传来的数据
      this.formObj = myrow //当前行数据 赋值给表单的v-model
      this.dBtn = '确认修改'
      this.dtitle = '修改管理员'
      this.adminFlag = true
    })
  },
  methods: {
    tosearch () {
      //发起查询
      this.$bus.$emit('sendsearch', this.searchText)
      this.searchText = ''
    },
    open () {
      //点击添加按钮
      this.adminFlag = true
      this.dBtn = '确认添加'
      this.dtitle = '添加管理员'
      this.formObj.userName = ''
      this.formObj.turename = ''
      this.formObj.phone = ''
      this.formObj.password = '123456'
      this.formObj.power = ''
    },
    close () {
      this.adminFlag = false
    },
    sureAdd () {
      //确认添加 或确认修改
      // 表单验证通过
      this.$refs.adminForm.validate(async vali => {
        console.log(vali)
        if (vali) {
          if (this.formObj.tokenId) {
            //编辑接口
            var EditRes = await _editAdmin(this.formObj)
            this.$message.success('编辑成功')
            console.log(EditRes)
          } else {
            //添加接口
            var res = await _AddAdmin(this.formObj)
            console.log(res)
            if (res.data.success) {
              this.$message.success('添加管理员成功')

              // 对整个表单进行重置初始化
            }
          }
          this.adminFlag = false //关闭弹出层
          this.$refs.adminForm.resetFields()
          // 触发获取列表方法
          this.$bus.$emit('sendsearch', '')
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.form_wrap {
  width: 80%;
  display: flex;
  margin-bottom: 30px;
  justify-content: space-between;
  .form_list {
    display: flex;
  }
}

// .mydate .el-date-table td.start-date span {
//   background: red !important;
// }
// .mydate .el-date-table td.end-date span {
//   background: red !important;
// }
</style>
